package com.meida.module.bus.provider.controller;

import cn.afterturn.easypoi.csv.CsvExportUtil;
import cn.afterturn.easypoi.csv.entity.CsvExportParams;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.bus.client.vo.AppSchoolVo;
import com.meida.module.user.client.entity.AppSchool;
import com.meida.module.user.provider.service.AppSchoolService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


/**
 * 学校控制器
 *
 * @author flyme
 * @date 2021-08-07
 */
@RestController
@RequestMapping("/school/")
public class SchoolController extends BaseController<AppSchoolService, AppSchool> {

    @PostMapping(value = "export")
    @ApiOperation(value = "学校-导出", notes = "学校-导出")
    public void export(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Map params) {
        try {
            CriteriaQuery<AppSchool> cq = new CriteriaQuery(params, AppSchool.class);
            cq.select(AppSchool.class, "schoolId", "schoolName", "fullLetter", "schoolAbridge", "emailSuffix");
            List<EntityMap> entityMapList = bizService.selectEntityMap(cq);
            List<AppSchoolVo> list = Lists.newArrayList();
            entityMapList.forEach(entityMap -> {
                Long schoolId = entityMap.getLong("schoolId");
                String schoolName = entityMap.get("schoolName");
                String fullLetter = entityMap.get("fullLetter");
                String schoolAbridge = entityMap.get("schoolAbridge");
                String emailSuffix = entityMap.get("emailSuffix");
                AppSchoolVo appSchoolVo = new AppSchoolVo();
                appSchoolVo.setSchoolId(String.valueOf(schoolId) + " ");
                appSchoolVo.setSchoolName(schoolName);
                appSchoolVo.setFullLetter(fullLetter);
                appSchoolVo.setSchoolAbridge(schoolAbridge);
                appSchoolVo.setEmailSuffix(emailSuffix);
                list.add(appSchoolVo);
            });
            CsvExportParams csvExportParams = new CsvExportParams();
            File tempFile = File.createTempFile("school", ".csv");
            FileOutputStream fos = new FileOutputStream(tempFile);
            CsvExportUtil.exportCsv(csvExportParams, AppSchoolVo.class, list, fos);
            File fileLoad = new File(tempFile.getCanonicalPath());
            FileInputStream in = new FileInputStream(fileLoad);
            exportFile(response, in, "平台学校.csv");
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void exportFile(HttpServletResponse response, FileInputStream in, String fileName) throws IOException {
        OutputStream out = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        response.setContentType("text/csv;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setCharacterEncoding("UTF-8");
        try {
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (FileNotFoundException e) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                    out.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
